<template>
  <div style="width: 500px; height: 500px; margin: 200px auto; padding: 20px; ">
    <el-card>
      <div style="margin: 30px">
        <div v-if="update"></div>
        <el-form label-width="100px">
          <el-upload style="text-align: center; padding-bottom: 10px" action="http://localhost:8081/file/upload" :show-file-list="false" :on-success="handleAvatarSuccess">
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>

          <el-form-item label="用户名">
            <el-input v-model="form.username" autocomplete="off" readonly></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item style="text-align: center">
            <el-button type="primary" @click="editForm">确 定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      form: {},
      user: localStorage.getItem("user") != null ? JSON.parse(localStorage.getItem("user")) : {},
      update: true,
    }
  },
  created() {
    this.request.get("/user/" + this.user.username).then(res => {
      if(res != null) {
        this.form = res;
        this.form.token = JSON.parse(localStorage.getItem("user")).token;
      }
    })
  },
  methods: {
    editForm() {
      this.request.put("/user", this.form).then(res => {
        if(res.data) {
          localStorage.setItem("user", JSON.stringify(this.form));
          this.$message.success(res.message);
        }
      });
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res;
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 120px;
  height: 120px;
  display: block;
}
</style>